package com.baseweb.webadmin.weixin.utils;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * http网络请求
 * 
 * @author wcc
 *
 */
public class HttpGetOrPost {
	private   static  Logger logeer = LoggerFactory.getLogger(HttpGetOrPost.class);
	//连接超时时间，默认10秒
    private static int socketTimeout = 10000;

    //传输超时时间，默认30秒
    private  static int connectTimeout = 30000;
	/**
	 * get请求 返回一个  JSONObject 对象
	 * @param url
	 * @return  正常返回json对象 异常返回null
	 */
	public static JSONObject sendGet(String url) {
		CloseableHttpClient httpclient = null ;
		CloseableHttpResponse response = null;
		try {
		   httpclient = HttpClients.createDefault();
			HttpGet httpget = new HttpGet(url);
			response = httpclient.execute(httpget);
			logeer.info("HttpGetOrPost ----> response"+response);
			HttpEntity entity = response.getEntity();
			logeer.info("entity"+response);
			if (entity != null) {
				String s = EntityUtils.toString(entity);
				logeer.info("HttpGetOrPost ---> entity  s"+s);
				JSONObject jsonobj =  JSON.parseObject(s);
				return jsonobj;
			}
		} catch (Exception e) {
			logeer.error("HttpGetOrPost-->Exception "+e);
			e.printStackTrace();
		} finally {
			if(response != null){
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(httpclient != null){
				try {
					httpclient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}
	
	

	/***
	 * https  post请求
	 * @param url
	 * @return 正常返回String字符串 异常返回null
	 */
	public static  String sendPost(String  postDataXML)  {
		CloseableHttpClient httpClient = null ;
		logeer.info("HttpGetOrPost  sendPost--->start ");
        HttpPost httpPost = new HttpPost(Constant.GET_PREPAYID);
        //指明使用UTF-8编码，否则到API服务器XML的中文不能被成功识别
        StringEntity postEntity = new StringEntity(postDataXML, "UTF-8");
        httpPost.addHeader("Content-Type", "text/xml");
        httpPost.setEntity(postEntity);
        //设置请求器的配置
        httpPost.setConfig(RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build());
        try {
        	httpClient = HttpClients.createDefault();
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            String result = EntityUtils.toString(entity, "UTF-8");
            logeer.info("HttpGetOrPost  sendPost---> entity："+result);
			return result;

        } catch (Exception e) {
        	 logeer.error("HttpGetOrPost  sendPost---> Exception ",e);
        } finally {
            httpPost.abort();
        }
        return null;
    }

}
